﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;

using ITSSP.Controls.ExtensionMethods;
using ITSSP.Views.UserList;
using ITSSP.Web.Models;
using ITSSP.Web.Services;

namespace ITSSP.Views
{
    public partial class GuestUserListPage : Page
    {
        private UserContext m_UserContext = new UserContext();

        public GuestUserListPage()
        {
            InitializeComponent();

            // load data
            this.userGrid.ItemsSource = m_UserContext.Users;
            m_UserContext.Load(m_UserContext.GetGuestUsersQuery());
        }

        private void addNewUser_Click(object sender, RoutedEventArgs e)
        {
            NewUserWindow newUser = new NewUserWindow();
			newUser.Show(delegate
			{
				if (newUser.DialogResult == true)
				{
					m_UserContext.Users.Add(newUser.NewUser);
					m_UserContext.SubmitChanges();
				}
			});
        }

		private void editCurUser_Click(object sender, RoutedEventArgs e)
		{
			EditUserWindow win = new EditUserWindow(this.userGrid.SelectedItem as User);
			win.Show(delegate
			{
				if (win.DialogResult == true)
				{
					m_UserContext.SubmitChanges();
				}
			});
		}

		private void pwdCurUser_Click(object sender, RoutedEventArgs e)
		{
			PasswordUserWindow win = new PasswordUserWindow(this.userGrid.SelectedItem as User);
			win.Show(delegate
			{
				if (win.DialogResult == true)
				{
					m_UserContext.SubmitChanges();
				}
			});
		}

        private void delCurUser_Click(object sender, RoutedEventArgs e)
        {
            User curUser = (User)this.userGrid.SelectedItem;
            if (curUser == null) return;

            DeleteMessageBox msg = new DeleteMessageBox();
            msg.UserToBeDeleted = curUser;
			msg.Show(delegate
			{
				if (msg.DialogResult == true)
				{
					m_UserContext.Users.Remove(msg.UserToBeDeleted);
					m_UserContext.SubmitChanges();
				}
			});
        }
	}
}
